package com.wsoft.bpm.controller;

import cn.hutool.core.convert.Convert;
import com.wsoft.bpm.entity.ActNoticeEntity;
import com.wsoft.bpm.query.run.TaskHistoryQuery;
import com.wsoft.bpm.service.IActNoticeService;
import com.wsoft.bpm.service.RunProcessService;
import com.wsoft.bpm.vo.*;
import com.wsoft.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.activiti.engine.task.Task;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhonglj
 * @date 2024-09-19 16:05
 **/
@RestController
@Tag(name = "工作流查询管理")
@RequestMapping("/bpm/query")
public class QueryProcessController {

    @Resource
    private RunProcessService runProcessService;
    @Resource
    private IActNoticeService noticeService;

    @Operation(summary = "知会列表", description = "知会列表")
    @GetMapping("/notice/list")
    public ResponseResult<List<ActNoticeEntity>> listNotice() {
        return ResponseResult.success(noticeService.unReadList());
    }

    @Operation(summary = "待办列表", description = "待办列表")
    @GetMapping("/todoList")
    public ResponseResult<List<TaskTodoVO>> todoList() {
        List<Task> tasks = runProcessService.todoList();
        List<TaskTodoVO> data = runProcessService.packageExtraTodoData(tasks);
        return ResponseResult.success(data);
    }

    @Operation(summary = "已办列表", description = "已办列表")
    @GetMapping("/doneList")
    public ResponseResult<List<TaskDoneVO>> doneList() {
        return ResponseResult.success(runProcessService.doneList().stream().map(e -> Convert.convert(TaskDoneVO.class, e)).collect(Collectors.toList()));
    }

    @Operation(summary = "审批历史", description = "审批历史")
    @GetMapping("/history")
    public ResponseResult<List<TaskHistoryVO>> history(@ParameterObject TaskHistoryQuery query) {
        return ResponseResult.success(runProcessService.history(query));
    }

    @Operation(summary = "获取跳转指定节点下拉框", description = "获取跳转指定节点下拉框")
    @GetMapping("/listApprovalNodes")
    public ResponseResult<List<TaskHistoryVO>> listApprovalNodes(@Parameter(description = "流程任务id") String taskId,
                                                                 @Parameter(description = "流程实例id") String processInstanceId) {
        List<TaskHistoryVO> taskHistoryVOS = runProcessService.listApprovalNodes(taskId, processInstanceId);
        return ResponseResult.success(taskHistoryVOS);
    }

    @Operation(summary = "获取审批节点数据", description = "获取审批节点数据")
    @GetMapping("/getNodeData")
    public ResponseResult<ApprovalNodeDataVO> getNodeData(@Parameter(description = "任务id") String taskId) {
        return ResponseResult.success(runProcessService.getNodeData(taskId));
    }

    @Operation(summary = "获取已经部署的流程定义列表", description = "获取已经部署的流程定义列表")
    @GetMapping("/listDeployedDef")
    public ResponseResult<List<DeployedDefVO>> listDeployedDef() {
        return ResponseResult.success(runProcessService.listDeployedDef());
    }

    @Operation(summary = "获取流程图", description = "获取流程图")
    @GetMapping("/taskImage")
    public ResponseResult taskImage(@Parameter(description = "流程任务id") String taskId,
                                    @Parameter(description = "流程实例id") String processInstanceId) {
        String img = "data:image/svg+xml;base64," + runProcessService.createXmlAndPngAtNowTask(taskId, processInstanceId);
        return ResponseResult.success(img, "[sys.msg.success]");
    }
}
